package com.mjh.controller;

import com.github.pagehelper.PageInfo;

import com.mjh.bean.Clue;
import com.mjh.bean.ClueRemark;
import com.mjh.model.R;
import com.mjh.service.ClueService;
import com.mjh.vo.ClueRemarkVo;
import com.mjh.vo.ClueVo;
import com.mjh.vo.PieData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 线索管理
 * @author mjh
 * @date 2021-10-21 21:08
 */
@RestController
@RequestMapping("/api/clue")
public class ClueController {
    @Autowired
    private ClueService clueService;


    /**
     * 分页查询线索
     * @return
     */
    @GetMapping("/page")
    public R<PageInfo<ClueVo>> pageInfo(@RequestParam("current") Integer current,
                                        @RequestParam(defaultValue = "10")  Integer size){
        PageInfo<ClueVo> pageInfo =  clueService.queryClueListPage(current,size);
        return R.OK(pageInfo);
    }
    /**
     * 新增线索
     * @return
     */
    @PostMapping
    public R<String> saveClue(@RequestBody Clue clue) {
        Boolean saved = clueService.saveClue(clue);
        return R.HANDLER(saved);
    }

    /**
     * 根据标识查询线索信息
     * @param clueId
     * @return
     */
    @GetMapping("/{clueId}")
    public R<Clue> loadClueInfo(@PathVariable Integer clueId) {
        return R.OK(clueService.queryClueInfoById(clueId));
    }

    /**
     * 编辑线索内容
     * @param clue
     * @return
     */
    @PutMapping
    public R<String> modifyClue(@RequestBody Clue clue) {
        Boolean modified = clueService.modifyClue(clue);
        return R.HANDLER(modified);
    }
    /**
     * 批量导入线索
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/import")
    public R<String> importClueExcel(MultipartFile file) throws IOException {
        clueService.batchSaveClue(file);
        return R.OK(null);
    }
    /**
     * 根据标识查询线索详情
     * @param id
     * @return
     */
    @GetMapping("/detail/info/{id}")
    public R<ClueVo> loadClueDetailInfo(@PathVariable Integer id) {
        ClueVo clueVo = clueService.queryClueDetailInfoById(id);
        return R.OK(clueVo);
    }

    /**
     * 单个删除线索
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public R<String> removeClueById(@PathVariable Integer id) {
        Boolean removed = clueService.removeClueById(id);
        return R.HANDLER(removed);
    }

    /**
     * 批量删除线索
     * @return
     */
    @DeleteMapping("/batch/{ids}")
    public R<String> removeClueByIds(@PathVariable List<Integer> ids) {
        Boolean removed = clueService.removeClueByIds(ids);
        return R.HANDLER(removed);
    }

    /**
     * 查询线索跟踪饼图数据
     * @return
     */
    @GetMapping("/noteway")
    public R<List<PieData>> loadNotewayList() {
        List<PieData> list = clueService.queryNotewayList();
        return R.OK(list);
    }

    /**
     * 线索转客户
     * @return
     */
    @PostMapping("/toCustomer/{clueId}")
    public R<String> transformToCustomer(@PathVariable Integer clueId) {
        Boolean transformed = clueService.transformToCustomer(clueId);
        return R.HANDLER(transformed);
    }
    /**
     * 查询线索备注列表
     * @return
     */
    @GetMapping("/name/list")
    public R<Set<ClueVo>> loadClueNameList() {
        Set<ClueVo> list = clueService.queryClueNameList();
        return R.OK(list);
    }




}
